package tn.orange.canreminder.adapter;

import java.util.List;

import tn.orange.canreminder.PlaceMapActivity;
import tn.orange.canreminder.R;
import tn.orange.canreminder.sqlite.Place;
import tn.orange.canreminder.sqlite.PlaceDao;
import tn.orange.canreminder.sqlite.PlaceDaoImp;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class PlaceFavoriteAdapter extends BaseAdapter {

	private Context context;
	private List<Place> places;
	private LayoutInflater mInflater;
	private Location location;

	public PlaceFavoriteAdapter(Context context, List<Place> places,
			Location location) {
		super();
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.places = places;
		this.location = location;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return places.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return places.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {

		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.place_favorite_tem1, null);
			holder = new ViewHolder();

			holder.nom = (TextView) convertView
					.findViewById(R.id.place_item_favorite_nom);
			holder.distance = (TextView) convertView
					.findViewById(R.id.place_item_favorite_distance);

			holder.box = (CheckBox) convertView
					.findViewById(R.id.place_ite_fav);

			holder.map = (ImageView) convertView
					.findViewById(R.id.place_item_favorite_map);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Place place = places.get(position);

		holder.nom.setText(place.getNom());
		holder.distance.setText("" + place.getDistance() + "m");
		holder.map.setTag(position);
		holder.box.setChecked(true);
		holder.box.setTag(position);

		// holder.btndel.setTag(place.getId());
		// holder.button.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(context, PlaceMapActivity.class);
		//
		// double latitude = Double.parseDouble(place.getLatitude());
		// double longitude = Double.parseDouble(place.getLongitude());
		//
		// intent.putExtra("longitude", longitude);
		// intent.putExtra("latitude", latitude);
		// Log.e("longitude", place.getLongitude());
		// Log.e("latitude", place.getLatitude());
		// intent.putExtra("nom", place.getNom());
		// context.startActivity(intent);
		// }
		// });

		return convertView;
	}

	static class ViewHolder {
		TextView nom;
		TextView distance;
		CheckBox box;
		ImageView map;

	}

}
